//	Copyright (c) 2008, x-port.net ltd.
//
//	All rights reserved.
//
//	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
//	    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//	    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//	    * Neither the name of the x-port.net ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
//	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
//	A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
//	CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
//	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
//	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
//	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
		function dropbox(oClosed,oOpen,oTrigger)
		{
			this.m_oClosed = oClosed;
			this.m_oContainer = oOpen;
			this.m_oTrigger = oTrigger;
			if(oTrigger)
				this.m_oTrigger = oTrigger;
			this.useShadow = true;
			this.useIFrame = false;
			this.animVert = 100;
			this.animHoriz = 0;
			this.animSpeed = 0.3;
			this._initContainer();
			this._initAnim();
			this._bContainerOpen = false;
			var pThis = this;
			
			var theListener = new ActiveXObject("DOM2Events.DispEventListener");
			theListener.handler = function(e)
			{
				var bToggle = (e.type == "DOMFocusIn");
				if(e.type == "DOMFocusOut" && pThis.m_oContainer.contains(document.activeElement))
				{
					e.stopPropagation();
					bToggle = true;
				}
				pThis._toggleContainer(bToggle);
				if(!bToggle)
				{
					try{
						pThis.m_oClosed.parentElement.focus();
					}catch(e)
					{
						//ignore - probably because parent is unfocusable.
					}
				}
			};
			
			this.m_oClosed.parentElement.addEventListener("DOMFocusIn",theListener,false);
			this.m_oClosed.parentElement.addEventListener("DOMFocusOut",theListener,false);
			this.m_oClosed.addEventListener("DOMFocusOut",theListener,false);
			this.m_oClosed.parentElement.addEventListener("fp-ui-select",theListener,false);
			var fnUnload = 	function(){
						oClosed.parentElement.addEventListener("DOMFocusIn",theListener,false);
						oClosed.parentElement.addEventListener("DOMFocusOut",theListener,false);
						oClosed.addEventListener("DOMFocusOut",theListener,false);
						oClosed.parentElement.addEventListener("fp-ui-select",theListener,false);
						delete theListener;
						theListener = null;
					};

			document.parentWindow.attachEvent("onbeforeunload",fnUnload);
		}


dropbox.prototype._initAnim = function()
{	

	var animSpeed = this.animSpeed;
    if((this.animHoriz || this.animVert) && YAHOO.util.Anim) {
        if(isNaN(animSpeed) || (animSpeed < 0)) {
            animSpeed = 0.3;
        }
        if(!this._oAnim ) {
            oAnim = new YAHOO.util.Anim(this.m_oContainer._oContent, {}, this.animSpeed);
            this._oAnim = oAnim;
        }
        else {
            this._oAnim.duration = animSpeed;
        }
    }
}

/**
 * Animates expansion or collapse of the container.
 *
 * @method _toggleContainer
 * @param bShow {Boolean} True if container should be expanded, false if container should be collapsed
 */
dropbox.prototype._toggleContainer = function(bShow) {
    var oContainer = this.m_oContainer;
	this.m_oContainer.style.left = this.m_oClosed.offsetLeft;
    // Implementer has container always open so don't mess with it
    if(bShow && this._bContainerOpen) {
        return;
    }
    
    // Clear contents of container

    // Container is already closed
    if (!bShow && !this._bContainerOpen) {
        oContainer._oContent.style.display = "none";
        //oContainer.style.display = "none";
        return;
    }

    // If animation is enabled...
    var oAnim = this._oAnim;
    if (oAnim && oAnim.getEl() && (this.animHoriz || this.animVert)) {

        if(oAnim.isAnimated()) {
            oAnim.stop();
        }
		
		this.m_oContainer.style.visibility = "hidden";
		this.m_oContainer.style.display = "block";
		var h = this.m_oContainer.style.height;
		this.m_oContainer._oContent.style.height = "auto";
        oContainer._oContent.style.display = "block";
		this.defaultContainerHeight = this.m_oContainer._oContent.offsetHeight;
		this.m_oContainer._oContent.style.height = h;
		
		//alert(this.defaultContainerHeight)
        var wExp = this.m_oClosed.offsetWidth;

        
        var hExp = this.absoluteContainerHeight?this.absoluteContainerHeight:this.defaultContainerHeight;
       // window.status = "w:" + wExp + " h:" + hExp;
        // Calculate COLLAPSED sizes based on horiz and vert anim
        var wColl = (this.animHoriz) ? 1 : wExp;
        var hColl = (this.animVert) ? 1 : hExp;

        // Set animation sizes

        oAnim.attributes = (bShow) ?
            {width: {from:wColl, to: wExp }, height: { to: hExp }} :
            {width: {from:wExp, to: wColl}, height: { to: hColl }};



       oContainer._oContent.style.height = "1px";
		
 		this.m_oContainer.style.visibility = "visible";
 		this.m_oContainer._oContent.style.visibility = "visible";

    	var oSelf = this;
    	var onAnimComplete = function() {
            // Finish the collapse
    		oAnim.onComplete.unsubscribeAll();

            if(bShow) {
            
			if(oContainer._oIFrame) {
					bFireEvent = true;
					var width = oContainer._oContent.offsetWidth + "px";
					var height = oContainer._oContent.offsetHeight + "px";
					oContainer._oIFrame.style.width = width;
					oContainer._oIFrame.style.height = height;
					
				}
				      }
            else {
                oContainer._oContent.style.display = "none";
				oContainer.style.visibility = "hidden";
            }
     	};

        // Display container and animate it
		if(bShow)
		{
	        oContainer._oContent.style.display = "block";
		}
        oAnim.onComplete.subscribe(onAnimComplete);
        oAnim.animate();
        this._bContainerOpen = bShow;
    }
    // Else don't animate, just show or hide
    else {
        if(bShow) {
		    oContainer.style.display = "block";
            oContainer._oContent.style.display = "block";
        }
        else {
		    oContainer.style.display = "none";
            oContainer._oContent.style.display = "none";
        }
        this._bContainerOpen = bShow;
   }

};


dropbox.prototype._initContainer = function() {
    if(!this.m_oContainer._oContent) {
    
        var oContent = document.createElement("div");
        oContent.className = "content";
        oContent.style.display = "none";
        oContent.style.height = "1px";
        this.m_oContainer.style.display = "block";
        this.m_oContainer.style.height = "1px";
        
        while(this.m_oContainer.hasChildNodes())
		{
			oContent.appendChild(this.m_oContainer.firstChild);
		}
        this.m_oContainer._oContent = this.m_oContainer.appendChild(oContent);
        
        if(this.useIFrame && !this.m_oContainer._oIFrame) {
        var oIFrame = document.createElement("iframe");
        oIFrame.src = "javascript:false";
        oIFrame.frameBorder = 0;
        oIFrame.scrolling = "no";
        oIFrame.style.position = "absolute";
        oIFrame.style.width = "100%";
        oIFrame.style.height = "100%";
        oIFrame.tabIndex = -1;
        this.m_oContainer._oIFrame = this.m_oContainer.appendChild(oIFrame);
    }
	}
    else {
    }
};